package com.yunke.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yunke.entity.dto.YunkeAdminDto;
import com.yunke.entity.dto.YunkeAdminLoginDto;
import com.yunke.entity.vo.YunkeAdminVo;
import com.yunke.common.result.YunkeResult;
import com.yunke.service.YunkeAdminService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import java.util.List;

/**
 * 管理员Controller
 * 
 * @author yunke
 * @since 1.0.0
 */
@Slf4j
@RestController
@RequestMapping("/api/admin")
@RequiredArgsConstructor
@Validated
@Tag(name = "管理员管理", description = "管理员相关接口")
public class YunkeAdminController {

    private final YunkeAdminService adminService;

    @PostMapping("/login")
    @Operation(summary = "管理员登录", description = "管理员账号密码登录")
    public YunkeResult<String> login(@Valid @RequestBody YunkeAdminLoginDto loginDto) {
        return adminService.login(loginDto);
    }

    @PostMapping("/logout")
    @Operation(summary = "管理员登出", description = "退出登录")
    public YunkeResult<Void> logout() {
        return adminService.logout();
    }

    @GetMapping("/current")
    @Operation(summary = "获取当前管理员信息", description = "获取当前登录管理员的详细信息")
    public YunkeResult<YunkeAdminVo> getCurrentAdmin() {
        return adminService.getCurrentAdmin();
    }

    @PutMapping("/password")
    @Operation(summary = "修改当前管理员密码", description = "修改当前登录管理员的密码")
    public YunkeResult<Void> changePassword(@Parameter(description = "原密码") @RequestParam String oldPassword,
                                      @Parameter(description = "新密码") @RequestParam String newPassword) {
        return adminService.changePassword(oldPassword, newPassword);
    }

    @PutMapping("/current")
    @Operation(summary = "更新当前管理员信息", description = "更新当前登录管理员的基本信息")
    public YunkeResult<Void> updateCurrentAdmin(@Valid @RequestBody YunkeAdminDto adminDto) {
        return adminService.updateCurrentAdmin(adminDto);
    }

    @PostMapping
    @SaCheckPermission("admin:create")
    @Operation(summary = "创建管理员", description = "创建新的管理员账户")
    public YunkeResult<Void> createAdmin(@Valid @RequestBody YunkeAdminDto adminDto) {
        return adminService.createAdmin(adminDto);
    }

    @PutMapping("/{id}")
    @SaCheckPermission("admin:update")
    @Operation(summary = "更新管理员", description = "更新指定管理员的信息")
    public YunkeResult<Void> updateAdmin(@Parameter(description = "管理员ID") @PathVariable Long id,
                                   @Valid @RequestBody YunkeAdminDto adminDto) {
        adminDto.setId(id);
        return adminService.updateAdmin(adminDto);
    }

    @DeleteMapping("/{id}")
    @SaCheckPermission("admin:delete")
    @Operation(summary = "删除管理员", description = "删除指定的管理员")
    public YunkeResult<Void> deleteAdmin(@Parameter(description = "管理员ID") @PathVariable Long id) {
        return adminService.deleteAdmin(id);
    }

    @DeleteMapping("/batch")
    @SaCheckPermission("admin:delete")
    @Operation(summary = "批量删除管理员", description = "批量删除指定的管理员")
    public YunkeResult<Void> batchDeleteAdmin(@Parameter(description = "管理员ID列表") @RequestBody @NotEmpty List<Long> ids) {
        return adminService.batchDeleteAdmin(ids);
    }

    @GetMapping("/{id}")
    @SaCheckPermission("admin:view")
    @Operation(summary = "获取管理员详情", description = "获取指定管理员的详细信息")
    public YunkeResult<YunkeAdminVo> getAdminDetail(@Parameter(description = "管理员ID") @PathVariable Long id) {
        return adminService.getAdminDetail(id);
    }

    @GetMapping("/page")
    @SaCheckPermission("admin:view")
    @Operation(summary = "分页查询管理员列表", description = "分页查询管理员列表")
    public YunkeResult<IPage<YunkeAdminVo>> getAdminPage(
            @Parameter(description = "当前页码") @RequestParam(defaultValue = "1") Integer current,
            @Parameter(description = "页面大小") @RequestParam(defaultValue = "10") Integer size,
            @Parameter(description = "用户名") @RequestParam(required = false) String username,
            @Parameter(description = "昵称") @RequestParam(required = false) String nickname,
            @Parameter(description = "账户状态") @RequestParam(required = false) Integer accountStatus,
            @Parameter(description = "角色ID") @RequestParam(required = false) Long roleId,
            @Parameter(description = "部门ID") @RequestParam(required = false) Long departmentId) {
        return adminService.getAdminPage(current, size, username, nickname, accountStatus, roleId, departmentId);
    }

    @PutMapping("/{id}/password")
    @SaCheckPermission("admin:reset-password")
    @Operation(summary = "重置管理员密码", description = "重置指定管理员的密码")
    public YunkeResult<Void> resetPassword(@Parameter(description = "管理员ID") @PathVariable Long id,
                                     @Parameter(description = "新密码") @RequestParam String newPassword) {
        return adminService.resetPassword(id, newPassword);
    }

    @PutMapping("/{id}/status")
    @SaCheckPermission("admin:update-status")
    @Operation(summary = "更新管理员状态", description = "更新指定管理员的账户状态")
    public YunkeResult<Void> updateAdminStatus(@Parameter(description = "管理员ID") @PathVariable Long id,
                                         @Parameter(description = "账户状态") @RequestParam @NotNull Integer accountStatus) {
        return adminService.updateAdminStatus(id, accountStatus);
    }

    @PutMapping("/batch/status")
    @SaCheckPermission("admin:update-status")
    @Operation(summary = "批量更新管理员状态", description = "批量更新管理员的账户状态")
    public YunkeResult<Void> batchUpdateAdminStatus(@Parameter(description = "管理员ID列表") @RequestParam @NotEmpty List<Long> ids,
                                              @Parameter(description = "账户状态") @RequestParam @NotNull Integer accountStatus) {
        return adminService.batchUpdateAdminStatus(ids, accountStatus);
    }

    @PutMapping("/{id}/unlock")
    @SaCheckPermission("admin:unlock")
    @Operation(summary = "解锁管理员账户", description = "解锁被锁定的管理员账户")
    public YunkeResult<Void> unlockAdmin(@Parameter(description = "管理员ID") @PathVariable Long id) {
        return adminService.unlockAdmin(id);
    }

    @GetMapping("/{id}/permissions")
    @SaCheckPermission("admin:view")
    @Operation(summary = "获取管理员权限列表", description = "获取指定管理员的权限列表")
    public YunkeResult<List<String>> getAdminPermissions(@Parameter(description = "管理员ID") @PathVariable Long adminId) {
        return adminService.getAdminPermissions(adminId);
    }

    @GetMapping("/count")
    @SaCheckPermission("admin:view")
    @Operation(summary = "统计管理员数量", description = "统计不同状态的管理员数量")
    public YunkeResult<Long> countAdmins(@Parameter(description = "账户状态") @RequestParam(required = false) Integer accountStatus) {
        return adminService.countAdmins(accountStatus);
    }
} 